/*
 * @Author: yixin 
 * @Date: 2019-12-29 16:41:45 
 * @Last Modified by: yixin
 * @Last Modified time: 2020-01-09 15:06:49
 * 设备画像公共方法
 */
import echarts from 'echarts/lib/echarts';
import { getChartColor } from './index';
// 文本化状态信息
export function formatStatus(type) {
  switch (type) {
    case 1:
      return '泵送'
      break;
    case 2:
      return '待料'
      break;
    case 3:
      return '转场'
      break;
    case 4:
      return '行驶'
      break;
    case 5:
      return '驻车'
      break;
    default:
      return '其他'
      break;
  }
}

// 状态颜色
export function formatColor(state) {
  return getChartColor[formatStatus(Number(state))]
}

// 状态时间
export function formatTime(time) {
  if(time<60){
    return `${time}秒`
  }else if(time<3600){
    return `${(time/60).toFixed(0)}分钟 ${(time%60)}秒`
  } else {
    return `${(time/3600).toFixed(2)}小时`
  }
}

// 获取url参数
export function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}

export function renderItem(params, api) {
  var categoryIndex = 0;
  var start = api.coord([api.value(1), categoryIndex]);
  var end = api.coord([api.value(2), categoryIndex]);
  var height = api.size([0, 1])[1];
  return {
      type: 'rect',
      shape: echarts.graphic.clipRectByRect({
          x: start[0],
          y: start[1] - height,
          width: end[0] - start[0],
          height: height
      }, {
          x: params.coordSys.x,
          y: params.coordSys.y,
          width: params.coordSys.width,
          height: params.coordSys.height
      }),
      style: api.style()
  };
}